﻿
Imports System.Reflection

Public Module _display
    Public Function display(ByVal s As String, Optional ByVal length As Int64 = 64) As String
        If strlen(s) > length Then
            Return strleft(s, length) + "..."
        Else
            Return s
        End If
    End Function

    Public Function display(ByVal i As IDisplay, Optional ByVal length As Int64 = 64) As String
        If Not i Is Nothing Then
            Return i.display(length)
        Else
            Return Nothing
        End If
    End Function

    Public Function display(Of T)(ByVal i As T, Optional ByVal length As Int64 = 64) As String
        If isDisplay(i) Then
            'as this function is usually for displaying text/log, so do not assert if convertor.cast failed.
            Return display(convertor.cast(Of IDisplay)(i, False), length)
#If RETIRED Then
            Try
                Dim displayMethod As MethodInfo = Nothing
                displayMethod = i.GetType().GetMethod("display", BindingFlags.Public _
                                                              Or BindingFlags.Instance _
                                                              Or BindingFlags.IgnoreCase)
                If Not displayMethod Is Nothing Then
                    Return display(displayMethod.Invoke(i, Nothing), maxInt64)
                End If
            Catch
            End Try
#End If
        Else
            Return display(Convert.ToString(i), length)
        End If
    End Function

    Public Function isDisplay(Of T)() As Boolean
        Dim i As T = Nothing
        i = alloc(Of T)()
        Return isDisplay(i)
    End Function

    Public Function isDisplay(Of T)(ByVal i As T) As Boolean
        Return Not i Is Nothing AndAlso TypeOf i Is IDisplay
    End Function
End Module

Public Interface IDisplay
    Function display(Optional ByVal length As Int64 = 64) As String
End Interface
